#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   SessionControl_0x10_test.py
@Time    :   2023/08/16 13:31:28
@Author  :   Panhong
@Version :   1.0
@Contact :   Panhong1@chengtech.com
@License :   (C)Copyright 2023-2026
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
import pytest
import logging
import time
from commDriver.TS_CAN import *
import allure
import sys
import Set_up

sys.dont_write_bytecode = True



class Test_Flash_stick_phy(object):

    @allure.severity('normal')
    @allure.story('Test Flash whether or not stick')
    @allure.description('Test Flash whether or not stick')
    @pytest.mark.repeat(100)
    def test_Flash_stick_Phy(self, action, Driver):
        iStep = 1
        with allure.step('Step1:Request:0x10 0x01, Response:0x50,0x01,0x00,0x32,0x01, 0xF4'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x01], 2)
            with assume:
                assert uds_rep[0:6] == [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4]
        with allure.step('Step2:Request:0x10 0x03, Response:0x50,0x03,0x00,0x32,0x01, 0xF4'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x03], 2)
            with assume:
                assert uds_rep[0:6] == [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4]
        Driver['CAN'].unlock(Driver, 3)
        with allure.step('Step2:Request:0x10 0x03, Response:0x50,0x03,0x00,0x32,0x01, 0xF4'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x2E, 0xF1,0XA1,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF], 11)
            with assume:
                assert uds_rep[0:3] == [0x6e, 0xf1, 0xa1]
        with allure.step('Step5:Request:0x10 0x02, Response:0x50,0x02,0x00,0x32,0x01, 0xF4'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x02], 2)
            with assume:
                assert uds_rep[0:6] == [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4]
        for i in range(10):
            logging.info(f'------------------第{i}次循环切换钥匙信号1->3->1----------------------')
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'Left_BCM_0x12D', 'BCMPower_Gear_12D_S', 3)
            time.sleep(0.1)
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'Left_BCM_0x12D', 'BCMPower_Gear_12D_S', 1)
            time.sleep(0.2)
            logging.info("先设置BCMPower_Gear_12D_S为OFF档,并等待1s".center(30, '*'))
            Driver['CAN'].write_db_signal(Set_up.NetName, Set_up.nodeName, 'Left_BCM_0x12D', 'BCMPower_Gear_12D_S', 3)
            time.sleep(0.1)
            with allure.step('Step2:Request:0x10 0x03, Response:0x50,0x03,0x00,0x32,0x01, 0xF4'):
                uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x01], 2)
                with assume:
                    assert uds_rep[0:6] == [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4]
            with allure.step('Step2:Request:0x10 0x03, Response:0x50,0x03,0x00,0x32,0x01, 0xF4'):
                uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x03], 2)
                with assume:
                    assert uds_rep[0:6] == [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4]
            Driver['CAN'].unlock(Driver, 3)
            with allure.step('Step2:Request:0x10 0x03, Response:0x50,0x03,0x00,0x32,0x01, 0xF4'):
                uds_rep, size = Driver['UDS'].req_and_res_can([0x2E, 0xF1,0XA1,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF], 11)
                with assume:
                    assert uds_rep[0:3] == [0x6e, 0xf1, 0xa1]
            iStep,uds_resp, size = Driver['CAN'].checkReqRespNoWait(Driver, '2E F1 90 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17', '6E F1 90', 0, iStep,False, 'UDS', 'noClearFault')
            with allure.step('Step5:Request:0x10 0x02, Response:0x50,0x02,0x00,0x32,0x01, 0xF4'):
                uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x02], 2)
                with assume:
                    assert uds_rep[0:6] == [0x50, 0x02, 0x00, 0x32, 0x01, 0xF4]
        with allure.step('Step8:Request:0x10 0x01, Response:0x50,0x01,0x00,0x32,0x01, 0xF4'):
            uds_rep, size = Driver['UDS'].req_and_res_can([0x10, 0x01], 2)
            with assume:
                assert uds_rep[0:6] == [0x50, 0x01, 0x00, 0x32, 0x01, 0xF4]